package edu.hawaii.myisern.action;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;

/**
 * Provides test cases for MyIsernActionBean.
 * 
 * @author Jon Lao
 * @author Jared Sunouchi
 * @author Chiao-Fen Zielinski-Liu
 */
public class TestMyIsernActionBean {

  /** Get the test host. */
  private String testHost = System.getProperty("test.host");
  /** A string representing the homepage title */
  private static final String HOMEPAGE_TITLE = "MyISERN 2.0 - Home";
  /** A string representing the login page title */
  private static final String LOGIN_TITLE = "MyISERN 2.0 - Login";
  /** A string represent the username attribute */
  private static final String USERNAME_FIELD = "username";
  /** A string represent the password attribute */
  private static final String PASSWORD_FIELD = "password";
  /** A string representing the user name */
  private static final String USERNAME = "username";
  /** A string representing the password */
  private static final String PASSWORD = "password";
  
  
  /**
   * Tests the IndexActionBean class.
   *
   * @throws Exception If problems occur
   */
  @Test
  public void testMyIsernActionBean() throws Exception {
    // prevent javascript errors from being thrown
    com.meterware.httpunit.HttpUnitOptions.setExceptionsThrownOnScriptError (false);
    com.meterware.httpunit.HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);    
    com.meterware.httpunit.HttpUnitOptions.setScriptingEnabled(false);
    
    WebConversation conversation = new WebConversation();

    // retrieves the index.jsp page and check the title
    String url = this.testHost + "myisern";
    WebResponse response = conversation.getResponse(url);
    assertEquals("Clicking link without login", LOGIN_TITLE, response.getTitle());
    
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest loginRequest = loginForm.getRequest();
    
    loginRequest.setParameter(USERNAME_FIELD, USERNAME);
    loginRequest.setParameter(PASSWORD_FIELD, PASSWORD);
    response = conversation.getResponse(loginRequest);
    assertEquals("Logged in", HOMEPAGE_TITLE, response.getTitle());
    
    WebLink link = response.getLinkWithID("displayCollaborations");
    response = link.click();
    assertEquals("Displaying collaborations", "MyISERN 2.0 - Display Collaborations",
        response.getTitle());
    
    link = response.getLinkWithID("displayOrganizations");
    response = link.click();
    assertEquals("Displaying organizations", "MyISERN 2.0 - Display Organizations",
        response.getTitle());
    
    link = response.getLinkWithID("displayResearchers");
    response = link.click();
    assertEquals("Displaying researchers", "MyISERN 2.0 - Display Researchers",
        response.getTitle());
    
    link = response.getLinkWithID("logout");
    link.click();
    response = conversation.getCurrentPage();
    assertEquals("Logged out", LOGIN_TITLE, response.getTitle());
    
    //assertTrue("shutting down", this.myModel.shutDown());
  }
}
